package com.zsk.shop.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zsk.shop.common.base.BaseResponse;
import com.zsk.shop.common.constant.RedisKeys;
import com.zsk.shop.common.utils.RedisUtil;
import com.zsk.shop.common.utils.ToolUtil;
import com.zsk.shop.entity.DeliveryAddressStatus;
import com.zsk.shop.enums.EnumDataDictionaryKey;
import com.zsk.shop.enums.EnumOrderStatus;
import com.zsk.shop.mapper.DeliveryAddressStatusMapper;
import com.zsk.shop.model.params.PackageOrderParams;
import com.zsk.shop.model.results.PackageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 快递信息
 *
 * @author Ys
 * @date 2021年12月07日 14:34
 */
@Service
@Slf4j
public class DeliveryAddressStatusService extends ServiceImpl<DeliveryAddressStatusMapper, DeliveryAddressStatus> {

    @Resource
    private ShopOrderService shopOrderService;

    public BaseResponse<PackageResult> getPackageList(String orderNo) {
        return BaseResponse.success(this.baseMapper.getPackageList(orderNo));
    }

    public BaseResponse queryNumber(String number) {
        DeliveryAddressStatus orderInfo = this.queryOneNumber(number);
        if (ToolUtil.isNotEmpty(orderInfo)) {
            return BaseResponse.error("该物流单号已添加:" + orderInfo.getNumber());
        }
        return BaseResponse.success();
    }

    public BaseResponse saveOrderNumber(PackageOrderParams params) {
        try {
            int count = this.baseMapper.getCount(params.getOrderNo());
            String smallPicture = RedisUtil.get(RedisKeys.dictionaryKey(EnumDataDictionaryKey.ORDER_PACKAGE.getKey())).toString();
            if (count >= Integer.parseInt(smallPicture)) {
                return BaseResponse.error("包裹只能填写" + smallPicture + "个");
            }
            String[] number = params.getNumber().split(",");
            for (int i = 0; i < number.length; i++) {
                DeliveryAddressStatus orderStatus = new DeliveryAddressStatus();
                orderStatus.setOrderNo(params.getOrderNo());
                orderStatus.setNumber(number[i]);
                orderStatus.setName("包裹" + (count + 1));
                count++;
                if (this.baseMapper.insertSelective(orderStatus) > 0) {
                    shopOrderService.updateOrderEndStatus(params.getOrderNo(), EnumOrderStatus.DELIVERY_STATUS.getCode());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return BaseResponse.error("服务器异常，请重试...");
        }
        return BaseResponse.success();
    }

    /**
     * 获取订单物流单号
     *
     * @param orderNo
     * @return
     */
    public DeliveryAddressStatus queryOne(String orderNo) {
        QueryWrapper<DeliveryAddressStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DeliveryAddressStatus::getOrderNo, orderNo);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    /**
     * 获取物流单号是否存在
     *
     * @param number
     * @return
     */
    public DeliveryAddressStatus queryOneNumber(String number) {
        QueryWrapper<DeliveryAddressStatus> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DeliveryAddressStatus::getNumber, number);
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

}
